package com.jiyi.activity.datainput.sm;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import com.google.gson.Gson;
import com.jiyi.activity.datainput.DIBaseMorePicActivity;
import com.jiyi.bean.SingleImageBean;
import com.jiyi.bean.datainput.new_input.BaseCheck;
import com.jiyi.bean.datainput.new_input.FireZoneBean;
import com.jiyi.exception.SampleProgressObserver;
import com.jiyi.inteface.DownloadPicOverInterface;
import com.jiyi.retrofit.HttpMethods;
import com.jiyi.widget.NoScrollGridView;
import com.swift.maintenance.R;
import com.swift.maintenance.base.AppConfig;
import com.swift.maintenance.base.AppMediaType;
import com.swift.maintenance.utils.ToastUtil;
import com.swift.maintenance.utils.UserInfoUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.MultipartBody;

/**
 * Created by Administrator on 2018/3/22.
 */

public class FireZoneActivity extends DIBaseMorePicActivity implements DownloadPicOverInterface {
    /* 头部 */
    @BindView(R.id.tvBack)
    TextView tvBack;
    @BindView(R.id.tvTitle)
    TextView tvTitle;
    @BindView(R.id.tvSearch)
    TextView tvSearch;

    @BindView(R.id.rbDensePlacesYes)
    RadioButton rbDensePlacesYes;
    @BindView(R.id.rbDensePlacesNo)
    RadioButton rbDensePlacesNo;
    @BindView(R.id.rgDensePlacesIsOK)
    RadioGroup rgDensePlacesIsOK;
    @BindView(R.id.etDensePlacesProblem)
    EditText etDensePlacesProblem;
    @BindView(R.id.etDensePlacesMeasure)
    EditText etDensePlacesMeasure;
    @BindView(R.id.rbFireDistrictYes)
    RadioButton rbFireDistrictYes;
    @BindView(R.id.rbFireDistrictNo)
    RadioButton rbFireDistrictNo;
    @BindView(R.id.rgFireDistrictHasUse)
    RadioGroup rgFireDistrictHasUse;
    @BindView(R.id.etFireDistrictProblem)
    EditText etFireDistrictProblem;
    @BindView(R.id.etFireDistrictMeasure)
    EditText etFireDistrictMeasure;
    @BindView(R.id.rbFireMeasuresYes)
    RadioButton rbFireMeasuresYes;
    @BindView(R.id.rbFireMeasuresNo)
    RadioButton rbFireMeasuresNo;
    @BindView(R.id.rgFireMeasuresIsOk)
    RadioGroup rgFireMeasuresIsOk;
    @BindView(R.id.etFireMeasuresProblem)
    EditText etFireMeasuresProblem;
    @BindView(R.id.etFireMeasuresMeasure)
    EditText etFireMeasuresMeasure;
    @BindView(R.id.etGeneratorLocation)
    EditText etGeneratorLocation;
    @BindView(R.id.etOilStorage)
    EditText etOilStorage;

    @BindView(R.id.gvDensePlaces)
    NoScrollGridView gvDensePlaces;
    @BindView(R.id.gvFireDistrict)
    NoScrollGridView gvFireDistrict;
    @BindView(R.id.rbFireMeasuresNoExist)
    RadioButton rbFireMeasuresNoExist;
    @BindView(R.id.gvFireMeasures)
    NoScrollGridView gvFireMeasures;

    String build_id = "";
    int mDensePlacesIsOK, mFireDistrictHasUse, mFireMeasuresIsOk;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fire_zone);
        ButterKnife.bind(this);
        init();
    }

    public void init() {
        getIntentData();
        setTop();
        setViewStatus();
        getData();
        super.setDownloadPicOverInterface(this);
    }


    private void getIntentData() {
        build_id = getIntent().getStringExtra("build_no");
        if (TextUtils.isEmpty(build_id))
            build_id = "";
    }

    private void setTop() {
        tvTitle.setText("防火分区与平面布置");
        tvSearch.setCompoundDrawables(null, null, null, null);
        tvSearch.setText("保存");
    }

    public void setViewStatus() {
        rgDensePlacesIsOK.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rbDensePlacesYes) {
                    mDensePlacesIsOK = 1;
                } else {
                    mDensePlacesIsOK = 0;
                }
            }
        });

        rgFireDistrictHasUse.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rbFireDistrictYes) {
                    mFireDistrictHasUse = 1;
                } else {
                    mFireDistrictHasUse = 0;
                }
            }
        });

        rgFireMeasuresIsOk.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == R.id.rbFireMeasuresYes) {
                    mFireMeasuresIsOk = 1;
                } else if (checkedId == R.id.rbFireMeasuresNo) {
                    mFireMeasuresIsOk = 0;
                }
                {
                    mFireMeasuresIsOk = 2;
                }
            }
        });
    }


    public void getData() {
        HttpMethods.getInstance().getFireZoneInfo(UserInfoUtil.getCurrentUserAccessToken(), build_id, 2)
                .subscribe(new SampleProgressObserver<FireZoneBean>(this) {
                    @Override
                    public void onNext(FireZoneBean fireZoneBean) {
                        if (fireZoneBean == null || "".equals(fireZoneBean.getGson_null())) {
                            fireZoneBean = new FireZoneBean();
                            BaseCheck bc1 = new BaseCheck();
                            bc1.setIs_ok(0);
                            bc1.setProblem("");
                            bc1.setMeasures("");
                            fireZoneBean.setComply_safety_require(bc1);
                            BaseCheck bc2 = new BaseCheck();
                            bc2.setIs_ok(0);
                            bc2.setProblem("");
                            bc2.setMeasures("");
                            fireZoneBean.setIs_occupy(bc2);
                            BaseCheck bc3 = new BaseCheck();
                            bc3.setIs_ok(0);
                            bc3.setProblem("");
                            bc3.setMeasures("");
                            fireZoneBean.setIs_comply_safety_require(bc3);
                            fireZoneBean.setGenerator_area("");
                            fireZoneBean.setOil_storage_num("");
                            fireZoneBean.setNo_edit(1);
                        }

                        fillData(fireZoneBean);
                    }
                });
    }

    private void fillData(FireZoneBean fireZoneBean) {
        if (fireZoneBean.getComply_safety_require().getIs_ok() == 1) {
            rbDensePlacesYes.setChecked(true);
        }

        etDensePlacesProblem.setText(fireZoneBean.getComply_safety_require().getProblem());
        etDensePlacesMeasure.setText(fireZoneBean.getComply_safety_require().getMeasures());

        if (fireZoneBean.getIs_occupy().getIs_ok() == 1) {
            rbFireDistrictYes.setChecked(true);
        }

        etFireDistrictProblem.setText(fireZoneBean.getIs_occupy().getProblem());
        etFireDistrictMeasure.setText(fireZoneBean.getIs_occupy().getMeasures());

        if (fireZoneBean.getIs_comply_safety_require().getIs_ok() == 1) {
            rbFireMeasuresYes.setChecked(true);
        } else if (fireZoneBean.getIs_comply_safety_require().getIs_ok() == 0) {
            rbFireMeasuresNo.setChecked(true);
        }


        etFireMeasuresProblem.setText(fireZoneBean.getIs_comply_safety_require().getProblem());
        etFireMeasuresMeasure.setText(fireZoneBean.getIs_comply_safety_require().getMeasures());

        System.out.println("地区是: " + fireZoneBean.getGenerator_area());

        etGeneratorLocation.setText(fireZoneBean.getGenerator_area());
        etOilStorage.setText(fireZoneBean.getOil_storage_num());

        if (fireZoneBean.getNo_edit() == 1) {
            tvSearch.setVisibility(View.VISIBLE);
        } else {
            tvSearch.setVisibility(View.INVISIBLE);
        }

        initGridViewList(fireZoneBean);

    }

    private void doPost() {
        Map<String, String> a1 = new HashMap<>();
        a1.put("is_ok", mDensePlacesIsOK + "");
        a1.put("problem", etDensePlacesProblem.getText().toString());
        a1.put("measures", etDensePlacesMeasure.getText().toString());

        Map<String, String> a2 = new HashMap<>();
        a2.put("is_ok", mFireDistrictHasUse + "");
        a2.put("problem", etFireDistrictProblem.getText().toString());
        a2.put("measures", etFireDistrictMeasure.getText().toString());

        Map<String, String> a3 = new HashMap<>();
        a3.put("is_ok", mFireMeasuresIsOk + "");
        a3.put("problem", etFireMeasuresProblem.getText().toString());
        a3.put("measures", etFireMeasuresMeasure.getText().toString());


        Map<String, String> a4 = new HashMap<>();
        a4.put("generator_area", etGeneratorLocation.getText().toString());

        Map<String, Map<String, String>> dataParams = new HashMap<>();
        dataParams.put("comply_safety_require", a1);
        dataParams.put("is_occupy", a2);
        dataParams.put("is_comply_safety_require", a3);
        dataParams.put("generator_area", a4);

        //创建JSON对象
        JSONObject jsonObject = new JSONObject();
        try {
            jsonObject.put("comply_safety_require",a1);
            jsonObject.put("is_occupy",a2);
            jsonObject.put("is_comply_safety_require",a3);
//            jsonObject.put("generator_area",etGeneratorLocation.getText().toString());
        } catch (JSONException e) {
            e.printStackTrace();
        }

//        System.out.println("json:" + new Gson().toJson(dataParams).toString());



        MultipartBody.Builder builder = new MultipartBody.Builder();

        putBody(builder, AppConfig.ACCESS_TOKEN, AppMediaType.TEXT, UserInfoUtil.getCurrentUserAccessToken());
        putBody(builder, "build_id", AppMediaType.TEXT, build_id);
        putBody(builder, "type", AppMediaType.TEXT, 1 + "");
        putBody(builder, "data", AppMediaType.TEXT, new Gson().toJson(dataParams));
//        System.out.println("json:" + jsonObject.toString());
//        putBody(builder, "data", AppMediaType.TEXT, jsonObject);
        putBody(builder, "generator_area", AppMediaType.TEXT, etGeneratorLocation.getText().toString());
        putBody(builder, "action_type", AppMediaType.TEXT, "partition_plane_layout");


        // 图片数组
        File file;
        int size = mulGridViewBean.getSingleImageList().size();
        for (int i = 0; i < size; i++) {
            for (int j = 0; j < mulGridViewBean.getSingleImageList().get(i).getImageList().size(); j++) {
                if (mulGridViewBean.getSingleImageList().get(i).getImageList().get(j).contains("http")) {
                    Log.e("ttt     ", mulGridViewBean.getSingleImageList().get(i).getImageList().get(j) + "\n");
                    continue;
                } else {
                    file = new File(mulGridViewBean.getSingleImageList().get(i).getImageList().get(j));
                    if (i == 0) {
                        putBody(builder, "image*comply_safety_require[]", AppMediaType.JPG, file);
                    } else if (i == 1) {
                        putBody(builder, "image*is_occupy[]", AppMediaType.JPG, file);
                    } else if (i == 2) {
                        putBody(builder, "image*is_comply_safety_require[]", AppMediaType.JPG, file);
                    } else {
                        continue;
                    }
                }
            }
        }

        builder.setType(MultipartBody.FORM);
        MultipartBody multipartBody = builder.build();

        HttpMethods.getInstance().postFireZoneInfo(multipartBody)
                .subscribe(new SampleProgressObserver<FireZoneBean>(this) {
                    @Override
                    public void onNext(FireZoneBean fireZoneBean) {
                        ToastUtil.getInstances().showMsg("保存成功");
                    }
                });
    }

    void initGridViewList(FireZoneBean fireZoneBean) {
        List<SingleImageBean> singleImageBeans = new ArrayList<SingleImageBean>();

        SingleImageBean s1 = new SingleImageBean();
        s1.setPosition(0);
        s1.setDelModel(false);
        s1.setGridView(gvDensePlaces);
        s1.setImageList(fireZoneBean.getComply_safety_require().getImage());
        singleImageBeans.add(s1);

        SingleImageBean s2 = new SingleImageBean();
        s2.setPosition(1);
        s2.setDelModel(false);
        s2.setGridView(gvFireDistrict);
        s2.setImageList(fireZoneBean.getIs_occupy().getImage());
        singleImageBeans.add(s2);

        SingleImageBean s3 = new SingleImageBean();
        s3.setPosition(2);
        s3.setDelModel(false);
        s3.setGridView(gvFireMeasures);
        s3.setImageList(fireZoneBean.getIs_comply_safety_require().getImage());
        singleImageBeans.add(s3);

        mulGridViewBean.setSingleImageList(singleImageBeans);
        refreshAdapter();
    }

    @OnClick({R.id.tvBack, R.id.tvSearch})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.tvBack:
                finish();
                break;
            case R.id.tvSearch:
                downloadImage();
                break;
            default:
                break;
        }
    }

    @Override
    public void post() {
        doPost();
    }
}
